package io.renren.modules.work.service.impl;

import io.renren.common.utils.ConfigConstant;
import io.renren.modules.work.entity.ScConsumablesEntity;
import io.renren.modules.work.entity.ScProjectEntity;
import io.renren.modules.work.service.ScConsumablesService;
import io.renren.modules.work.service.ScProjectService;
import io.renren.vo.ScProjectEntityVo;
import io.renren.vo.StockTranVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.tomcat.jni.User;
import org.jfree.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.work.dao.ScStocktranDao;
import io.renren.modules.work.entity.ScStocktranEntity;
import io.renren.modules.work.service.ScStocktranService;
import org.springframework.transaction.annotation.Transactional;


@Service("scStocktranService")
@Slf4j
public class ScStocktranServiceImpl extends ServiceImpl<ScStocktranDao, ScStocktranEntity> implements ScStocktranService {

    @Autowired
    private ScConsumablesService scConsumablesService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //时间判断
        String from = null;
        String to = null;
        if(StringUtils.isNotBlank((String) params.get("from"))&&StringUtils.isNotBlank((String) params.get("to"))){
            from = sdf.format( Long.parseLong((String) params.get("from")));
            to = sdf.format( Long.parseLong((String) params.get("to")));
        }
        String table = (String)params.get("table");

        String stocktrantype = (String)params.get("stocktrantype");
        IPage<StockTranVo> result = null;
        if(table.equals("sc_project")){
             result = baseMapper.selectStockTranVoList4Project(new Query<StockTranVo>().getPage(params),Integer.parseInt(stocktrantype),
                     (String)params.get("goodsname"),from,to,
                     (String)params.get("projectno"));
        }else if(table.equals("sc_pur")){
             result = baseMapper.selectStockTranVoList4Pur(new Query<StockTranVo>().getPage(params),Integer.parseInt(stocktrantype),
                     (String)params.get("goodsname"),from,to,
                     (String)params.get("purno"));
        }
        return new PageUtils(result);
    }

    @Override
    @Transactional
    public void generateStockTran(String consumablesid) {
        ScConsumablesEntity scConsumablesEntity = scConsumablesService.getById(consumablesid);
        if(scConsumablesEntity!=null){
                ScStocktranEntity scStocktranEntity = new ScStocktranEntity();
                scStocktranEntity.setStocktrantype(ConfigConstant.STOCK_OUT);
                scStocktranEntity.setGoodsname(scConsumablesEntity.getGoodsname());
                scStocktranEntity.setIfstore(scConsumablesEntity.getIfstore());
                scStocktranEntity.setNum(scConsumablesEntity.getNum());
                scStocktranEntity.setResourceid(scConsumablesEntity.getProjectid());
                scStocktranEntity.setResourcetable("sc_project");
                scStocktranEntity.setCretime(new Date());
                baseMapper.insert(scStocktranEntity);
        }
        log.info("生成出库记录成功,工程单id：{}",scConsumablesEntity.getProjectid());

    }

}